<template>
  <view class="login-index">
    <view>
      <view class="login-img">
        <image src="./images/logo.png" mode="scaleToFill" />
      </view>
      <view class="login-title">亿车商</view>
    </view>
    <view class="chat-btn">
      <button
        v-show="userFlag"
        type="primary"
        open-type="getUserInfo"
        @getUserInfo="handleUserInfo"
      >
        获取用户信息
      </button>
      <button
        v-show="!userFlag"
        type="primary"
        open-type="getPhoneNumber"
        @getPhoneNumber="handlePhoneNumber"
      >
        微信账号快捷登录
      </button>
    </view>
  </view>
</template>

<script>
import Taro, { getCurrentInstance } from '@tarojs/taro'
import { login, getSession } from '@requests/user'
import { formatParams, getParam } from '@utils/'

export default {
  name: 'Login',
  data() {
    return {
      from: '',
      head_img: '',
      userFlag: true
    }
  },
  created() {
    this.from = decodeURIComponent(getParam('from'))
  },
  methods: {
    handleUserInfo(e) {
      this.head_img = e.detail.userInfo.avatarUrl
      this.userFlag = false
    },
    handlePhoneNumber(e) {
      const { iv } = e.detail
      if (!iv) {
        return
      }
      wx.login({
        success: res => {
          getSession(res.code).then(res => {
            login({
              head_img: this.head_img,
              session_key: res.result.session_key,
              iv,
              encrypted_data: e.detail.encryptedData
            }).then(res => {
              const {
                name,
                mobile,
                user_id,
                vip_level,
                personal_auth_status,
                head_img,
                token
              } = res.result
              const userInfo = {
                user_id,
                name,
                mobile,
                vip_level,
                personal_auth_status,
                head_img
              }
              this.$store.commit('user/setToken', token)
              this.$store.commit('user/setUserInfo', userInfo)
              this.loginSuccess(token)
            })
          })
        }
      })
    },
    loginSuccess(token) {
      Taro.setStorage({
        key: 'token',
        data: JSON.stringify({
          token,
          expire: new Date().getTime() + 86400000 * 30
        })
      })

      const homePages = ['/pages/index/index', '/pages/user/index']
      if (homePages.some(item => this.from.startsWith(item))) {
        Taro.switchTab({
          url: this.from
        })
      } else {
        Taro.redirectTo({
          url: this.from
        })
      }
    }
  }
}
</script>

<style>
.login-index {
  margin-top: 30%;
}
.login-img {
  text-align: center;
}
.login-img image {
  width: 120rpx;
  height: 120rpx;
}
.login-title {
  text-align: center;
  font-size: 1.02rem;
  margin-top: 0.2rem;
}
.chat-btn {
  text-align: center;
  margin-top: 140px;
}
.chat-btn button {
  width: 60%;
  font-size: 0.965rem;
}
</style>
